/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var seednia = seednia || {};

Backbone.Model.prototype.idAttribute = "sid";

seednia.Comment = Backbone.Model.extend({
	idAttribute: "sid",
	// Default attributes for the Comment
	defaults: {
		// Ensure that each photo created has an `src`.
		sid: -2
	},
	clear: function() {
      this.destroy();
    }
});

seednia.CommentCollection = Backbone.Collection.extend({
	model: seednia.Post,
	initialize : function(models, criteria) {
		this.postSid = criteria.postSid;
	}
});

seednia.Post = Backbone.Model.extend({
	idAttribute: "sid",
	url : "http://localhost/seednia-uiphp-nb/rspost.php/post/",
	
	defaults: {
		// Ensure that each photo created has an `src`.
		sid: -2,
		projectSid: -2,
		projectName: ""
	},

	initialize: function(){
	// 
	},
	clear: function() {
      this.destroy();
    }
});

seednia.PostCollection = Backbone.Collection.extend({

	// Reference to this collection's model.
	model: seednia.Post,
	initialize : function(models, criteria) {
		this.projectSid = criteria.projectSid;
	},
	url : function() {
		return "http://localhost/seednia-uiphp-nb/rspost.php/project/post/" + this.projectSid;
	}
});


seednia.PostView = Backbone.View.extend({

	//... is a list tag.
	tagName:  "li",
	className:  "post-entry",

	// Pass the contents of the photo template through a templating
	// function, cache it for a single photo
	template: _.template($('#post-template').html()),

	// The DOM events specific to an item.
	events: {
		"click img" : "imageClicked",
		"click a.delete" : "deletePost"
	},

	// The PhotoView listens for changes to its model, re-rendering. Since there's
	// a one-to-one correspondence between a **Photo** and a **PhotoView** in this
	// app, we set a direct reference on the model for convenience.
	initialize: function() {
		//_.bindAll(this, 'render');
		//this.model.bind('reset', this.render, this);
		this.model.on('reset', this.render, this);
		this.model.on('change', this.render, this);
		this.model.on('destroy', this.remove, this);
		//this.model.bind('destroy', this.remove, this); // bind is for backward compatibility
	},

	// Re-render the photo entry
	render: function() {
		this.$el.html(this.template(this.model.toJSON()));
		return this;
	},

	imageClicked: function() {
		alert(this.model.get("creatorDisplayName"));
	},
	
	deletePost: function() {
      this.model.clear();
    }

});

seednia.PostListView = Backbone.View.extend({
	    el: $("#posts_container"),
	 
	    initialize: function () {
		        this.render();
		this.collection.bind('reset', this.addAll);
	    },
	 
	    render: function () {
		        var that = this;
		        _.each(this.collection.models, function (item) {
			            that.renderPost(item);
		        }, this);
	    },
	 
	    renderPost: function (item) {
		        var _postView = new seednia.PostView({
			            model: item
		        });
		        this.$el.append(_postView.render().el);
	    }
});

//var _postListView = new seednia.PostView({ el: $("#post_contsiner") });
